#include "ClientMoon.h"

ClientMoon::ClientMoon(int pmoonId, vector<string> initVector)
{
	moonId = pmoonId;
	init(initVector);
}

void ClientMoon::setProduction(int pProduction)
{
	production = pProduction;
	int leftOver = (reproduction+production+research)-100;
	//lets see...it's reproduction + production + research must equal 100
	//leftOver is the amount that we are over
	//to make it easy, we could set reproduction
	//reproduction = reproduction - leftOver;
	//but that's too basic
	int leftOver1 = 0;
	int leftOver2 = 0;
	if (leftOver % 2 == 1 || leftOver % 2 == -1)
	{
		leftOver = leftOver - 1;
		leftOver1 = 1;
	}
	leftOver1 += (leftOver/2);
	leftOver2 += (leftOver/2);
	reproduction -= leftOver1;
	research -= leftOver2;
	if(reproduction < 0)
	{
		research += reproduction;
		reproduction = 0;
	}
	else if (research < 0)
	{
		reproduction += research;
		research = 0;
	}
	allocatePopulation(reproduction, research, production);
}

void ClientMoon::setReproduction(int pReproduction)
{
	reproduction = pReproduction;
	int leftOver = (reproduction+production+research)-100;
	//lets see...it's reproduction + production + research must equal 100
	//leftOver is the amount that we are over ( could be negative)
	//to make it easy, we could set reproduction
	//reproduction = reproduction - leftOver;
	//but that's too basic
	int leftOver1 = 0;
	int leftOver2 = 0;
	if (leftOver % 2 == 1 || leftOver % 2 == -1)
	{
		leftOver = leftOver - 1;
		leftOver1 = 1;
	}
	leftOver1 += (leftOver/2);
	leftOver2 += (leftOver/2);
	production -= leftOver1;
	research -= leftOver2;
	if(production < 0)
	{
		research += production;
		production = 0;
	}
	else if (research < 0)
	{
		production += research;
		research = 0;
	}
	allocatePopulation(reproduction, research, production);
}

void ClientMoon::setResearch(int pResearch)
{
	research = pResearch;
	int leftOver = (reproduction+production+research)-100;
	//lets see...it's reproduction + production + research must equal 100
	//leftOver is the amount that we are over
	//to make it easy, we could set reproduction
	//reproduction = reproduction - leftOver;
	//but that's too basic
	int leftOver1 = 0;
	int leftOver2 = 0;
	if (leftOver % 2 == 1 || leftOver % 2 == -1)
	{
		leftOver = leftOver - 1;
		leftOver1 = 1;
	}
	leftOver1 += (leftOver/2);
	leftOver2 += (leftOver/2);
	reproduction -= leftOver1;
	production -= leftOver2;
	if(production < 0)
	{
		reproduction += production;
		production = 0;
	}
	else if (reproduction < 0)
	{
		production += reproduction;
		reproduction = 0;
	}
	allocatePopulation(reproduction, research, production);
}

void ClientMoon::allocatePopulation(int pReproduction, int pProduction, int pResearch)
{
	//do comm request to update these values
}

Missile * ClientMoon::getReadyMissile()
{
	return readyMissile;
}

Shield * ClientMoon::getReadyShield()
{
	return readyShield;
}

void ClientMoon::init(vector<string> initVector)
{
	vector<string>::iterator it2 = (initVector).begin();
	//ignore first element "Moon"
	it2++;
	//ignore second element, moonId
	it2++;
	playerId = atof((*it2).c_str());
	it2++;
	population = atof((*it2).c_str());
	it2++;
	missileResearchLevel = atof((*it2).c_str());
	it2++;
	shieldResearchLevel = atof((*it2).c_str());
	it2++;
	reproduction = atof((*it2).c_str());
	it2++;
	production = atof((*it2).c_str());
	it2++;
	research = atof((*it2).c_str());
	it2++;
	currentProductionTaskName = *it2;
	it2++;
	researchProgress = atof((*it2).c_str());
	it2++;
	currentResearchTaskName=*it2;
	it2++;
	buildProgress = atof((*it2).c_str());
	it2++;
	int missilePower = atof((*it2).c_str());
	if(missilePower == -1)
	{
		//remove the current missile
		readyMissile = NULL;
	}
	else
	{
		//if there is a missile, don't do anything, otherwise create one
		if(readyMissile == NULL)
		{
			readyMissile = new Missile(missilePower);
		}
	}
	it2++;
	int shieldPower = atof((*it2).c_str());
	if(shieldPower == -1)
	{
		//remove the current missile
		readyShield = NULL;
	}
	else
	{
		//if there is a missile, don't do anything, otherwise create one
		if(readyShield == NULL)
		{
			readyShield = new Shield(shieldPower);
		}
		else
		{
			if((*readyShield).getPower() != shieldPower)
			{
				readyShield = new Shield(shieldPower);
			}
		}
	}
}